package com.kc.business.service.impl;

import cn.hutool.core.date.DateUtil;
import com.kc.business.mapper.BizBasicIndicatorsMapper;
import com.kc.business.mapper.BizGoodsMapper;
import com.kc.business.service.IBizBasicIndicatorsService;
import com.kc.common.utils.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName BizBasicIndicatorsService
 * @Description 根据获取指定日期注册用户数量 service 实现
 * @Author wangdongliang
 * @Date 2021/6/1 10:25
 */
@Service
public class BizBasicIndicatorsServiceImpl implements IBizBasicIndicatorsService {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private BizBasicIndicatorsMapper bizBasicIndicatorsMapper;
    @Override
    public int getUserNumByDate(String date,int dateType) {
        String key = "basic:indicators:"+date+":userNum";
        Object obj = redisTemplate.opsForValue().get(key);
        if(obj == null) {
            String currDate = DateUtil.now().substring(0, 10);
            int userNum = bizBasicIndicatorsMapper.getUserNumByDate(date,dateType);
            int expire = 300;
            if (currDate.equals(date)) {
                expire = getExpireTime();
            }
            redisTemplate.opsForValue().set(key, userNum, expire, TimeUnit.SECONDS);
            return userNum;
        }else{
            return Integer.parseInt(obj.toString());
        }
    }

    @Override
    public BigDecimal getOrderMoneyByDate(String date) {
        String orderMoney = bizBasicIndicatorsMapper.getOrderMoneyByDate(date);
        if(StringUtils.isEmpty(orderMoney)){
            orderMoney = "0";
        }
        return new BigDecimal(orderMoney);
    }

    public int getExpireTime(){
        int max = 7200;
        int min = 3600;
        Random rand = new Random();
        int randNumber =rand.nextInt(max - min + 1) + min;
        return randNumber;
    }


}
